require 'net/http'
require 'uri'
require 'rubygems'
require 'hpricot'
require 'open-uri'

 
def getRedirect ourl
	#puts ourl
	Net::HTTP.get_response(URI.parse(ourl))['location']	
end
#puts getRedirect('http://www.taobangwang.cn/taoapi.aspx?pid=6366189&sid=12821814&c=40')

def getTable url
	 # load the RedHanded home page
	 doc = Hpricot(open(url))	 
	 table = doc/"#ctl00_ContentHolder_MySortUserControl_GridViewTaobao"
	 top = 0;
	 trs = (table/"tr")
	 trs.each do |tr|
		if top>10
			tr.inner_html= ' '
			next
		end
		ts= (tr/"/td,/th")
			
		for i in 0...ts.length
			if i==1 
				ts[i].set_attribute :style,'{border-bottom:1px dashed #CDCDCD;text-decoration:none;font-weight:bold;font-family:Tahoma,SimSun,Arial;color:#0044DD;}'
			elsif  i==4 
				ts[i].innerHTML = ('<samp style="{color:#FF6600;display:block;font-size:13px;font-weight:700;}">'+ ts[i].innerHTML + '</samp>') if top>0
				ts[i].set_attribute :style,'{border-bottom:1px dashed #CDCDCD;}'
			elsif (i==5 or i==6 or i==8 ) 
				ts[i].set_attribute :style,'{display:none;}' 
			else 
				ts[i].set_attribute :style,'{border-bottom:1px dashed #CDCDCD;}'
			end
		end
		if top==0 
			tr.innerHTML = ('<th width="40" style="{border-bottom:1px dashed #CDCDCD;}">排名</th>') + tr.innerHTML 
		else
			tr.innerHTML = ('<td style="{border-bottom:1px dashed #CDCDCD;}"><samp style="{color:#FF6600;display:block;font-size:13px;font-weight:700;}">'+top.to_s()+"</samp></td>") + tr.innerHTML 
		end
		top = top+1
	 end
	 (table/"//a").each do |a|
		href = 'http://www.taobangwang.cn/'+a.attributes['href']
		href = getRedirect(href).sub(/\d{8}/,'11575228')
		a.set_attribute :href,href
	 end
	 tablehead = '<table style="{font-size:12px;>'
	 return tablehead +table.html+'</table>'
end
#File.new("table.html", "w+").puts getTable('http://www.taobangwang.cn/qq.aspx')
File.new("table.html", "w+").puts getTable('http://www.taobangwang.cn/lady.aspx')
